app.controller("readCatalogCtrl",function($scope,$state,$ionicLoading,$http,$ionicSlideBoxDelegate,$stateParams,$sce){
    var user_info = JSON.parse(localStorage.getItem("UserInfo"));
    var queryDate = localStorage.getItem("queryDate");
    var preReadTime = localStorage.getItem("preReadTime");
    var bookInfo = JSON.parse(localStorage.getItem("bookInfo"));
    // var getPath = JSON.parse(localStorage.getItem("getPath"));
    // var imgPath = JSON.parse(localStorage.getItem("imgPath"));
    angular.element(document).ready(function(){
        $scope.bookName = user_info.bookName;
        if(!$stateParams.queryDate) {
            $scope.getCard($stateParams.queryDate);
        }else{
            $scope.getCard(queryDate);
        }

    });

    //获取书籍卡片
    $scope.getCard = function (queryDate) {
        //获取学习计划
        var url = getPath.url+getPath.name+"/app/getCycleCard";
        var param = {
            userId:user_info.userId,
            bookId:bookInfo.bookId,
        }
        if(!queryDate){
            param.queryDate = new Date().Format("yyyy-MM-dd");
        }else{
            param.queryDate = queryDate;
        }
        $http({
            url: url,
            method: "POST",
            headers:{'Content-Type':'application/x-www-form-urlencoded'},
            data: param
        }).success(function (data) {
                if (data.resultCode == 200) {
                    var res = data.resultBean;
                    //书籍简介
                    var book = res.bookInfoDTO;
                    $scope.bookName = book.bookName;
                    $scope.remarks = book.remarks;
                    if(book.bookImg){
                        $scope.bookImg = imgPath.url+book.bookImg;
                    }
                    $scope.bookSummary = book.bookSummary;
                    //阅读时间
                    $scope.preReadTime = preReadTime;
                    var card = res.cycleDetailCardDTO;
                    //百分比
                    $scope.cardReadRate = Math.round((card.cardReadRate*100)) || 0;
                    //目录
                    var cycleDetaiList = res.cycleDetaiList;
                    var _len = cycleDetaiList.length;
                    //章节名称数组
                    var chapterNames = [];
                    var tempStr = "";
                    for(var i=0;i<_len;i++){
                        var temp = cycleDetaiList[i];
                        if(tempStr.indexOf(temp.chapterName)==-1){
                            tempStr = temp.chapterName;
                            chapterNames.push(temp);
                        }
                    }
                    $scope.chapterNames = chapterNames;
                    //章节下的卡片目录
                    $scope.cardList = res.cycleDetaiList;
                    //内容卡片内容卡片
                    var cards = res.bookCardList;
                    var _len = cards.length;
                    for(var i=0;i<_len;i++){
                        if(cards[i].cardImg){
                            cards[i].cardImg = imgPath.url+"/assets/upload/image/"+cards[i].cardImg;
                        }
                        if(cards[i].cardContent.length>100){
                            var cardContent = cards[i].cardContent.replace(/<[^>]+>/g,"").replace(/[ ]|[&nbsp;]/g, '');;
                            cards[i].cardContent = cardContent.substr(0,50)+"...";
                            // cards[i].cardContent = cards[i].cardContent.substr(0,50)+"...";
                        }
                    }
                    $scope.cards = cards;
                    $scope.cardIndex = _len;
                    $ionicSlideBoxDelegate.update();
                } else {
                    $ionicLoading.show({
                        template: "获取阅读章节失败",
                        duration: 3000
                    });
                }
            })
            .error(function () {
                $ionicLoading.hide();
                $ionicLoading.show({
                    template: "获取阅读章节失败",
                    duration: 3000
                });
            });
    }
    //阅读卡片详情页
    $scope.openBook = function(chapter){
        localStorage.setItem("chapter",JSON.stringify(chapter));
        $state.go("home.read-book",
            {
                cardNumber:chapter.cardNumber,
                taskDate:chapter.taskDate
            }
        );
    }
    //目录详情页
    $scope.openCatalogList = function () {
        $state.go("home.read-catalog-list",
            {
                queryDate:$stateParams.queryDate
            }
        );
    }
});
app.directive('scrollHeight',function(){
    return{
        restrict:'AE',
        link:function(scope,element){
            element[0].style.height=(340-53-20-34)+'px';
        }
    }
});